package 定时任务;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import 多线程篇.ThreadDemo;
import 多线程篇.线程池demo;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableScheduling
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
public class 定时任务demo{

    public static void main(String[] args) {
        SpringApplication.run(定时任务demo.class, args);
    }


    @Component
    public class OrderTimeOutCancelTask {
        private Logger LOGGER = LoggerFactory.getLogger(OrderTimeOutCancelTask.class);

        /**
         * cron表达式：Seconds Minutes Hours DayofMonth Month DayofWeek [Year]
         * 每10分钟扫描一次，扫描设定超时时间之前下的订单，如果没支付则取消该订单
         */
        @Scheduled(cron = "0/5 * * * * ?")
        private void cancelTimeOutOrder() {
            //scheduled本身自己也是一条线程
            //LOGGER.info("我是定时任务,线程是："+Thread.currentThread().getName());

            OnTimeBackUpDatabase.OnTimeBackUpDatabase();
        }
    }

}

class OnTimeBackUpDatabase implements Runnable{

    public static void OnTimeBackUpDatabase() {
        ThreadPoolExecutor poolExecutor = 线程池demo.poolExecutor;
        poolExecutor.submit(new OnTimeBackUpDatabase());
    }

    @Override
    public void run() {
        System.out.println("定时备份数据库，我是线程:"+Thread.currentThread().getName());
    }
}